package com.jader.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.jader.activities.AddressUpdateActivity;
import com.jader.beans.Address;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class AddressAdapter extends BaseAdapter {
	List<Address> address_list = new ArrayList<Address>();
	Context context;
	private LayoutInflater inflater;
	private String url = JaderApplication.getIP() + "/Jader/client";
	private HttpUtils httpUtils;
	String addressid;

	public AddressAdapter(List<Address> list, Context context) {
		super();
		this.address_list = list;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return address_list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return address_list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	ViewHolder viewHolder;

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.listviw_address, null);
			viewHolder = new ViewHolder();
			viewHolder.tv_address_name = (TextView) convertView
					.findViewById(R.id.tv_Address_name);
			viewHolder.tv_address_phone = (TextView) convertView
					.findViewById(R.id.tv_Address_phone);
			viewHolder.tv_address_info = (TextView) convertView
					.findViewById(R.id.tv_Address_info);
			viewHolder.btn_address_edit = (Button) convertView
					.findViewById(R.id.btn_Address_Edit);
			viewHolder.btn_address_delete = (Button) convertView
					.findViewById(R.id.btn_Address_Delete);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		String receiver = address_list.get(position).getReceiver();
		viewHolder.tv_address_name.setText(receiver);
		String province = address_list.get(position).getProvince();
		String city = address_list.get(position).getCity();
		String area = address_list.get(position).getArea();
		String detail = address_list.get(position).getDetail();
		String info = province + city + area + detail;
		viewHolder.tv_address_info.setText(info);
		String phone = address_list.get(position).getPhone();
		viewHolder.tv_address_phone.setText(phone);
		// 按钮单击事件
		viewHolder.btn_address_edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, AddressUpdateActivity.class);
				intent.putExtra("address", address_list.get(position));
				context.startActivity(intent);

			}
		});
		viewHolder.btn_address_delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				addressid = "" + address_list.get(position).getAddressid();
				addressDelete(position);
			}
		});
		return convertView;
	}

	/** 删除地址 */
	protected void addressDelete(final int location) {
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "" + JaderApplication.ADDRESS_DELETE);
		params.addBodyParameter("addressid", addressid);
		httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> info) {
						String result = info.result;
						if (result.equals("delete")) {
							JaderApplication.show(context, "删除成功");
							address_list.remove(location);
							notifyDataSetChanged();
						} else {
							JaderApplication.show(context, "再试一次吧");
						}
					}
				});
	}

	class ViewHolder {
		TextView tv_address_name, tv_address_phone, tv_address_info;
		Button btn_address_delete, btn_address_edit;
	}
}
